package org.diysearch.helpers;

import java.util.List;

import org.diysearch.dao.interfaces.CategoryDao;
import org.diysearch.models.Category;
import org.diysearch.tools.BaseTool;

public abstract class CategoryHelper {
	private static final CategoryDao dao = (CategoryDao) BaseTool.IOC_ENGINE.getBean("categoryDao");
	
	public static int save(String name, int parentId) throws Exception {
		Category c = new Category();
		c.setName(name);
		c.setParentId(parentId);
		return dao.save(c);
	}
	
	public static int delete(String name) throws Exception {
		Category c = new Category();
		c.setName(name);
		
		Category _c = dao.findByName(name);
		int code = LinkHelper.move(_c.getId(), 0); // move links out of this category
		return dao.delete(_c);
	}
	
	public static List<Category> fetchAll () throws Exception {
		return dao.findByParent(0);
	}
}
